if (project.properties.skipDistributions == "true")
{
    log.info("Skipping distribution")
    return
}

// splash
log.info '*' * 80
log.info('Aggregating files from distribution'.center(80))
log.info '*' * 80

def muleRoot = "$project.build.directory/../../../"

log.info("Mule project root: ${new File(muleRoot).canonicalPath}")

def buildDir = new File("$project.build.directory")
if (buildDir.exists() == false)
{
    buildDir.mkdirs()
}

// agregate all spring.schemas files into one
def fileScanner = ant.fileScanner
{
    fileset(dir: muleRoot) 
    {
        include(name: "**/spring.schemas")
        exclude(name: "**/target/**")
    }
}
new File("$project.build.directory/spring.schemas").withWriter
{ writer ->

    fileScanner.each
    { file ->

        writer.writeLine(file.text)
    }
}

// aggregate all spring.handlers files into one
fileScanner = ant.fileScanner
{
    fileset(dir: muleRoot) 
    {
        include(name: "**/spring.handlers")
        exclude(name: "**/target/**")
    }
}
new File("$project.build.directory/spring.handlers").withWriter
{ writer ->

    fileScanner.each
    { file ->

        writer.writeLine(file.text)
    }
}

// aggregate all custom.property.editors files into one
fileScanner = ant.fileScanner
{
    fileset(dir: muleRoot) 
    {
        include(name: "**/mule.custom-property-editors")
        exclude(name: "**/target/**")
    }
}
new File("$project.build.directory/mule.custom-property-editors").withWriter
{ writer ->

    fileScanner.each
    { file ->

        writer.writeLine(file.text)
    }
}

// aggregate all registry-bootstrap.properties into one
fileScanner = ant.fileScanner
{
    fileset(dir: muleRoot)
    {
        include(name: "**/registry-bootstrap.properties")
        exclude(name: "**/target/**")
    }
}
int objectCounter = 1
int transformerCounter = 1
Properties accumulatedProps = new Properties()

fileScanner.each
{ file ->

    Properties props = new Properties()
    props.load(file.newInputStream())

    for (Map.Entry entry : props.entrySet())
    {
        if (entry.getKey().startsWith("object"))
        {
            // avoid class cast exceptions when storing the properties object below
            def key = "object.${objectCounter++}".toString()
            // Make all objects optional to avoid CNF at startup (see MULE-5293)
            if (!entry.getValue().endsWith("optional"))
            {
                accumulatedProps.put(key, entry.getValue() + ",optional")
            }
        }
        else if (entry.getKey().startsWith("transformer"))
        {
            // avoid class cast exceptions when storing the properties object below
            def key = "transformer.${transformerCounter++}".toString()
            // Make all objects optional to avoid CNF at startup (see MULE-5293)
            if (!entry.getValue().endsWith("optional"))
            {
                accumulatedProps.put(key, entry.getValue() + ",optional")
            }
        }
        else
        {
            // we allow arbitrary keys in the registry-bootstrap.properties but since we're 
            // aggregating multiple files here we must make sure that the keys are unique
            if (accumulatedProps.getProperty(entry.getKey()) != null)
            {
                throw new IllegalStateException(
                    "more than one registry-bootstrap.properties file contains a key " + entry.getKey())
            }
            else
            {
                // Make all objects optional to avoid CNF at startup (see MULE-5293)
                if (!entry.getValue().endsWith("optional"))
                {
                    accumulatedProps.put(entry.getKey(), entry.getValue() + ",optional")
                }
            }
        }
    }
}

OutputStream out = new File("$project.build.directory/registry-bootstrap.properties").newOutputStream()
accumulatedProps.store(out, "Autogenerated by the Mule build")
